# -*- coding: utf-8 -*-

#    Copyright (C) 2005, 2006, 2007 2008, 2009, 2010, 2011 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>
#    This file is part of Promogest.

#    Promogest is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    Promogest is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with Promogest.  If not, see <http://www.gnu.org/licenses/>.

import datetime
from sqlalchemy import *
from sqlalchemy.orm import *
from promogest.dao.StaticPages import StaticPages
from promogest.lib.webutils import *
from promogest import Environment
from promogest.pages import *
from promogest.lib.session import Session
from promogest.lib.page import Page
from promogest.lib.unipath.path import *


def staticPages(req, first="", second=""):
    def staticPagesList(req, first=None, second=None):
        staticPages = StaticPages(req=req).select(batchSize=None)

        pageData = {'file' : 'staticPagesAction',
                    'staticPages' : staticPages}
        return Page(req).render(pageData)

    def staticPagesAdd(req, first=None, second=None):
        """Aggiunge una pagina a contenuto statico"""
        staticPages = None
        idrr = Session(req).getUserId()
        #userEnvDir= prepareUserEnv(req)
        #fileList = os.listdir(userEnvDir)
        if req.args.get('new') =="":
            staticPages = StaticPages(req=req)
            pageData = {'file' : 'staticPagesAction',
                        'staticPages' : staticPages,}
            return Page(req).render(pageData)
        elif req.args.get("upload"):
            pass
        elif req.args.get("edit"):
            staticPages = StaticPages(req=req).getRecord(id = int(req.args.get("edit")))
            pageData = {'file' : 'staticPagesAction',
                        'staticPages' : staticPages}
            return Page(req).render(pageData)
        elif req.args.get('add') == ""\
                        and req.form.get('pageTitle') \
                        and req.form.get('pageBody'):
                staticPages = StaticPages(req=req)
        elif req.args.get('update')\
                        and req.form.get('pageTitle') \
                        and req.form.get('pageBody'):
                staticPages = StaticPages(req=req).getRecord(id =int(req.args.get("update")))
        if staticPages:
            staticPages.title = req.form.get('pageTitle')
            staticPages.body = req.form.get('pageBody')
            #staticPages.abstract = req.form.get('newsAbstract')
            #staticPages.abstract = ""
            staticPages.permalink = permalinkaTitle(req.form.get('pageTitle'))
            staticPages.publication_date = datetime.datetime.utcnow()
            #if req.form.get('languageId'):
                #languageId = req.form.get('languageId')
                #if languageId != "None":
                    #staticPages.id_language = languageId
                #else:
                    #staticPages.id_language = 1
            staticPages.id_user = idrr
            #staticPages.persist()
            Environment.params["session"].add(staticPages)
            Environment.params["session"].commit()
        redirectUrl = '/staticPages/staticPagesList'
        return Page(req).redirect(redirectUrl)
        #return staticPagesList(req, first=first, second=second)


    def staticPagesDel(req, first="", second=None):
        """Cancella una pagina a contentuto statico"""
        pageId = req.args.get('pageId')
        staticPages = StaticPages(req=req).getRecord(id=pageId)
        staticPages.delete()
        redirectUrl='/staticPages/staticPagesList'
        return Page(req).redirect(redirectUrl)


    if second.upper() =="STATICPAGESLIST": # sezione Pagine
        return staticPagesList(req, first=first, second=second)
    elif second.upper() == "STATICPAGESDEL":
        return staticPagesDel(req, first=first, second=second)
    elif second.upper() == "STATICPAGESADD":
        return staticPagesAdd(req, first=first, second=second)
    elif second.upper() == "STATICPAGESACTIVE":
        return staticPagesActive(req, first=first, second=second)
    elif second.upper() == "STATICPAGESEDIT":
        return staticPagesEdit(req, first=first, second=second)
    else:
        return staticPagesList(req, first=first, second=action)
